package com.hand.ui;

import com.hand.db.Conn;
import com.hand.excel.ExcelUtil;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.sql.Connection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainWindow extends Application {
    ExecutorService executorService = Executors.newFixedThreadPool(1);

    Connection conn = null;
    File file = null;

    @Override
    public void start(Stage primaryStage) throws Exception {
        //网格布局
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        //网格垂直间距
        grid.setHgap(10);
        //网格水平间距
        grid.setVgap(10);
        grid.setPadding(new Insets(25, 25, 25, 25));
        //新建场景
        Scene scene = new Scene(grid, 1000, 800);
        primaryStage.setScene(scene);
        //添加标题
        Text scenetitle = new Text("创建连接-》选择文件-》输入下标-》开始导入");
        scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
        grid.add(scenetitle, 0, 0, 2, 1);


        //地址
        Label url = new Label("地址:");
        grid.add(url, 0, 1);

        TextField urlTextField = new TextField();
        grid.add(urlTextField, 1, 1);
        urlTextField.setText("jdbc:oracle:thin:@//10.107.213.41:15010/rbspitdb");
        //用户
        Label user = new Label("用户:");
        grid.add(user, 0, 2);

        TextField userTextField = new TextField();
        grid.add(userTextField, 1, 2);
        userTextField.setText("rbsnewuat");

        //密码
        Label pw = new Label("密码:");
        grid.add(pw, 0, 3);

        PasswordField pwBox = new PasswordField();
        grid.add(pwBox, 1, 3);

        //创建连接
        Button testbtn = new Button("创建连接");
        HBox testhbBtn = new HBox(10);
        testhbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        testhbBtn.getChildren().add(testbtn);
        grid.add(testhbBtn, 0, 4);

        testbtn.setOnAction(event -> {
            String urlString = urlTextField.getText();
            String username = userTextField.getText();
            String password = pwBox.getText();

            try {
                conn = Conn.getconn(urlString, username, password);
                if (null != conn) {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setContentText("连接成功");
                    alert.show();
                } else {
                    Alert alert = new Alert(Alert.AlertType.ERROR);
                    alert.setContentText("连接失败");
                    alert.show();
                }
            } catch (Exception e) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setContentText("连接失败");
                alert.show();
            }


        });

        //关闭连接
        Button disbtn = new Button("关闭连接");
        HBox dishbBtn = new HBox(10);
        dishbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        dishbBtn.getChildren().add(disbtn);
        grid.add(dishbBtn, 1, 4);

        disbtn.setOnAction(event -> {
            try {
                conn.close();
                conn = null;
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setContentText("关闭成功");
                alert.show();

            } catch (Exception e) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setContentText("关闭失败");
                alert.show();
            }
        });


        // 文件选择
        Button filebtn = new Button("选择文件");
        HBox filehbBtn = new HBox(10);
        filehbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        filehbBtn.getChildren().add(filebtn);
        grid.add(filehbBtn, 0, 5);

        TextField filePathField = new TextField();
        grid.add(filePathField, 1, 5);

        filebtn.setOnAction(event -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("选择文件");
            file = fileChooser.showOpenDialog(primaryStage);
            System.out.println(file.getAbsolutePath());
            filePathField.setText(file.getName());
        });

        Label infoLabel = new Label("数据位于Excel第几列（0开始）");
        grid.add(infoLabel, 0, 6,2,1);
        //发票行标识
        Label invoiceLabel = new Label("发票行标识:");
        grid.add(invoiceLabel, 0, 7);
        grid.add(MainWindowsComponent.getInstance().getInvoiceFlag(), 1, 7);
        MainWindowsComponent.getInstance().getInvoiceFlag().setText("2");

        //产品
        Label productLabel = new Label("产品:");
        grid.add(productLabel, 0, 8);
        grid.add(MainWindowsComponent.getInstance().getProduct(), 1, 8);
        MainWindowsComponent.getInstance().getProduct().setText("4");

        //线条
        Label lineLabel = new Label("线条:");
        grid.add(lineLabel, 0, 9);
        grid.add(MainWindowsComponent.getInstance().getLine(), 1, 9);
        MainWindowsComponent.getInstance().getLine().setText("5");



        //添加提交按钮
        Button btn = new Button("开始导入");
        HBox hbBtn = new HBox(10);
        hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        hbBtn.getChildren().add(btn);
        grid.add(hbBtn, 0, 10);

        btn.setOnAction(event -> {
            if (conn == null) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setContentText("没有数据库连接");
                alert.show();
                return;
            }
            if (file == null) {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setContentText("未选择文件");
                alert.show();
                return;
            }

            executorService.submit(() -> {
                try {
                    ExcelUtil.readByLine(file.getAbsolutePath(), conn);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });

        });


        grid.add(MainWindowsComponent.getInstance().getInfoArea(), 0, 11,3,6);


        primaryStage.setTitle("JavaFX Welcome");
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
